//
//  JJMultitermRecordCell.m
//  Estate
//
//  Created by houzhen on 2017/11/10.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "JJMultitermRecordCell.h"

@implementation JJMultitermRecordCell
- (void)setIsEdit:(BOOL)isEdit{
    if(isEdit){
        self.content_1.delegate=self;
        self.content_2.delegate=self;
        self.content_3.delegate=self;
        self.content_4.delegate=self;
        self.content_5.delegate=self;
        self.content_6.delegate=self;
    }else{
        self.content_1.userInteractionEnabled=NO;
        self.content_2.userInteractionEnabled=NO;
        self.content_3.userInteractionEnabled=NO;
        self.content_4.userInteractionEnabled=NO;
        self.content_5.userInteractionEnabled=NO;
        self.content_6.userInteractionEnabled=NO;
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.titleLabel.textColor=UIColorFromRGB(0x797979);
    self.titleLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    self.content_1.keyboardType = UIKeyboardTypeNumberPad;
    self.content_2.keyboardType = UIKeyboardTypeNumberPad;
    self.content_3.keyboardType = UIKeyboardTypeNumberPad;
    self.content_4.keyboardType = UIKeyboardTypeNumberPad;
    self.content_5.keyboardType = UIKeyboardTypeNumberPad;
    self.content_6.keyboardType = UIKeyboardTypeNumberPad;

}

- (IBAction)content_1Change:(ESTextField *)sender {
    if(self.content_1ChangeBlock){
        self.content_1ChangeBlock(self.content_1.text);
    }
}
- (IBAction)content_2Change:(ESTextField *)sender {
    if(self.content_2ChangeBlock){
        self.content_2ChangeBlock(self.content_2.text);
    }
}
-(IBAction)content_3Change:(ESTextField *)sender{
    if(self.content_3ChangeBlock){
        self.content_3ChangeBlock(self.content_3.text);
    }
}
-(IBAction)content_4Change:(ESTextField *)sender{
    if(self.content_3ChangeBlock){
        self.content_3ChangeBlock(self.content_3.text);
    }
}
-(IBAction)content_5Change:(ESTextField *)sender{
    if(self.content_5ChangeBlock){
        self.content_5ChangeBlock(self.content_5.text);
    }
}
-(IBAction)content_6Change:(ESTextField *)sender{
    if(self.content_6ChangeBlock){
        self.content_6ChangeBlock(self.content_6.text);
    }
}
#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.content_1 resignFirstResponder];
    [self.content_2 resignFirstResponder];
    [self.content_3 resignFirstResponder];
    [self.content_4 resignFirstResponder];
    [self.content_5 resignFirstResponder];
    [self.content_6 resignFirstResponder];
    return  YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //   限制苹果系统输入法  禁止输入表情
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    BOOL isHaveDian = YES;
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
        isHaveDian = NO;
    }
    
    //输入的字符是否是小数点
    if ([string length] > 0) {
        unichar single = [string characterAtIndex:0];//当前输入的字符
        if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确
            //判断长度
            if(!isHaveDian && [textField.text length] >= 10){
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
            //首字母不能为0和小数点
            if([textField.text length] == 0){
                if(single == '.') {
                    [self showError:@"第一个数字不能为小数点"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                if (single == '0') {
                    [self showError:@"第一个数字不能为0"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            //输入的字符是否是小数点
            if (single == '.') {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian = YES;
                    return YES;
                }else{
                    [self showError:@"您已经输入过小数点了"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }else{
                if (isHaveDian) {//存在小数点
                    //判断小数点的位数
                    NSRange ran = [textField.text rangeOfString:@"."];
                    if (range.location - ran.location <= 2) {
                        return YES;
                    }else{
                        [self showError:@"最多输入两位小数"];
                        return NO;
                    }
                }else{
                    return YES;
                }
            }
        }else{//输入的数据格式不正确
            [self showError:@"输入的格式不正确"];
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    return YES;
}

-(void)showError:(NSString *)msg{
    
    [LSUtility showToastWithMessage:msg inView:[UIApplication sharedApplication].keyWindow Andtime:1];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}



@end
